<?php
/**
 * block abstract class
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
abstract class BlockComponent extends CWidget{

    //Do not delete these attr block, page and errors
    /**
     *
     * @var string
     */
    public $id = 'html';
    /**
     *
     * @var Block
     */
    public $block = null;
    public $errors = array();
    public $page = null;

    protected $outputView = '';

    public function __construct($owner = null) {
        parent::__construct($owner);

        $this->outputView = "common.blocks.{$this->id}.block_output";
    }

    public function run() {
        //set params first
        $params = unserialize($this->block->params);
        foreach($this->params() as $key => $name){
            $this->{$key} = isset($params[$key]) ? $params[$key] : null;
        }
    }

    /**
     * get all attributes
     */
    abstract function params();

    /**
     * validate attribute
     * @return  boolean
     */
    abstract function validate();

    public function beforeBlockSave(){
        return true;
    }

    public function afterBlockSave(){
        return true;
    }

    /**
     * set properties from params
     * @param array $params $key => $param
     */
    public function setParams($params){
        $properties = $this->params();

        foreach($properties as $key => $name){
            $this->{$key} = isset($params[$key]) ? $params[$key] : null;
        }
    }
}